package com.fh.controller.loantask;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fh.annotation.SysLog;
import com.fh.util.*;
import com.ybd.common.mask.MaskTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.model.Page;
import com.fh.service.loantaskimpl.LoanTaskOverServiceImpl;
import com.fh.service.loantaskimpl.LoanTaskToBeServiceImpl;
import com.fh.service.system.organization.OrganizationServiceImpl;
import com.fh.service.system.user.UserService;

import tk.mybatis.mapper.util.StringUtil;

@Controller
@RequestMapping(value = "/loanTaskOver")
public class LoanTaskOverController extends BaseController {

    @Autowired
    private LoanTaskOverServiceImpl loanTaskOverService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrganizationServiceImpl organizationService;

    @Autowired
    private LoanTaskToBeServiceImpl loanTaskToBeService;
    /**
     * 已结案列表
     *
     * @param request
     * @param page
     * @return
     * @throws Exception
     */

    @RequestMapping(value = "/loanTaskOverListPage")
    public ModelAndView loanTaskOverList(HttpServletRequest request, Page page) throws Exception {
        ModelAndView mv = new ModelAndView();

        PageData pageData = loanTaskToBeService.setPdWithTrim(this.getPageData());

        Map<String, String> map1 = UserUtils.getCurrentRole();
        Integer QX1 = Integer.valueOf(map1.get(Const.SESSION_QX1));

        Map<String, Object> map = new HashMap<>();
        page.setPd(UserUtils.setDataRole(pageData, userService));
        String overdueStage = pageData.getString("overdueStageOver");//逾期阶段
        if (null != overdueStage && !"".equals(overdueStage)) {
            overdueStage = overdueStage.trim();
            pageData.put("overdueStageOver", overdueStage);
        }

        String overValue = pageData.getString("overValue");//逾期金额排序
        String overValueByDay = pageData.getString("overValueByDay");//逾期天数排序
        String sortbylasttime = pageData.getString("sortbylasttime");
        List<PageData> pageList = null;

        if (!StringUtil.isEmpty(overValue)) {
            if (overValue.equals("0")) {
                pageData.put("overValue", "cli.actual_recover_amount,cli.id");
                mv.addObject("overValueByAmt", 0);
            } else if (overValue.equals("1")) {
                pageData.put("overValue", "cli.actual_recover_amount DESC,cli.id");
                mv.addObject("overValueByAmt", 1);
            }
        } else if (!StringUtil.isEmpty(overValueByDay)) {
            if (overValueByDay.equals("0")) {
                pageData.put("overValue", "cli.overdue_date,cli.id");
                mv.addObject("overValueByDate", 0);
            } else if (overValueByDay.equals("1")) {
                pageData.put("overValue", "cli.overdue_date DESC,cli.id");
                mv.addObject("overValueByDate", 1);
            }
        }else if (!StringUtil.isEmpty(sortbylasttime)) {
            if (sortbylasttime.equals("0")) {
                pageData.put("overValue", "cli.last_call_time,cli.id");
                mv.addObject("sortbylasttime", 0);
            } else if (sortbylasttime.equals("1")) {
                pageData.put("overValue", "cli.last_call_time DESC,cli.id");
                mv.addObject("sortbylasttime", 1);
            }
        } else {
            pageData.put("overValue", "cli.create_time DESC,cli.id");
        }
        //pd.put("map", map);
        page.setPd(pageData);
        
        R.setPageHelper(pageData);
        pageList = loanTaskOverService.callTaskOverList(page);
        R.setPage(page, pageList);
        Map<String, MaskTypeEnum> properties = new HashMap<>();
        properties.put(Const.TELE_PHONE, MaskTypeEnum.MOBILE);
        properties.put(Const.CONT_ID_CARD2, MaskTypeEnum.IDCARD2);
        if (pageList.size() != 0) CoverUtil.setListCover(pageList, properties);//加密
        mv.addObject("pageList", pageList);
        mv.addObject("pd", pageData);
        mv.addObject(Const.SESSION_QX1, QX1);
        mv.setViewName("loantask/loanTaskOverList");

        return mv;
    }

    /**
     * 已结案导出
     *
     * @param response
     * @param page
     * @throws IOException
     */
    @SysLog("任务管理-结案Excel导出")
    @RequestMapping(value = "/outputExcel.do")
    public ModelAndView outputExcel(HttpServletResponse response, Page page) throws IOException {
    	
    	ModelAndView mv = this.getModelAndView();
        PageData pageData = loanTaskToBeService.setPdWithTrim(this.getPageData());

        Map<String, Object> map = new HashMap<>();
        String overdueStage = pageData.getString("overdueStage");//逾期阶段
        if (null != overdueStage && !"".equals(overdueStage)) {
            overdueStage = overdueStage.trim();
            pageData.put("overdueStage", overdueStage);
        }
        page.setPd(UserUtils.setGroupRole(pageData, organizationService));
        page.setPd(pageData);
        map=loanTaskOverService.callLoanOverExl(response, page);
        
        ObjectExcelView erv = new ObjectExcelView("逾期客户表-已结案");
		mv = new ModelAndView(erv,map);
		return mv;
    }
}
